package com.hanhai.zrb.backend.admin.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hanhai.zrb.backend.admin.dao.mybatis.PrivilegeDao;
import com.hanhai.zrb.backend.admin.dao.mybatis.RolePrivilegeDao;
import com.hanhai.zrb.backend.admin.model.Privilege;
import com.hanhai.zrb.backend.admin.service.PrivilegeService;
import com.hanhai.zrb.backend.common.model.PageVo;
import com.hanhai.zrb.backend.util.MybatisPageHelper;

@Service("privilegeService")
public class PrivilegeServiceImpl implements PrivilegeService {

	@Autowired
	private PrivilegeDao privilegeDao;

	@Autowired
	private RolePrivilegeDao rolePrivilegeDao;

	@Override
	public void add(Privilege privilege) {
		privilegeDao.add(privilege);

	}

	@Override
	public void update(Privilege privilege) {
		privilegeDao.update(privilege);
	}

	@Override
	public PageVo listPage(PageVo page) {
		MybatisPageHelper.setPageVo(page);
		List<Map<String, Object>> list = privilegeDao.listPage();
		page.setRows(list);
		return page;
	}

	@Override
	public Map<String, Object> getDetail(Integer id) {
		return privilegeDao.get(id);
	}

	@Override
	public List<Map<String, Object>> listByRole(Integer id) {
		return rolePrivilegeDao.listByRole(id);
	}

	@Override
	public boolean delete(Integer acl) {
		boolean succeed = true;
		List<Map<String, Object>> list = rolePrivilegeDao.listByPrivilege(acl);
		List<Map<String, Object>> child = privilegeDao.listChild(acl);
		if (list != null && list.size() > 0) {
			succeed = false;
		} else if (child != null && child.size() > 0) {
			succeed = false;
		} else {
			privilegeDao.delete(acl);
		}
		return succeed;
	}

	@Override
	public List<Map<String, Object>> listAll() {
		return privilegeDao.listAll();
	}
}
